package com.jrgiii.mbcr;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class TrainLocations extends ItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> myOverlays ;
	private Resources res;
	private MBCRActivity caller;

	public TrainLocations(Resources _res, Drawable defaultMarker, MBCRActivity _caller) {
		super(boundCenter(defaultMarker));
		myOverlays = new ArrayList<OverlayItem>();
		res = _res;
		caller = _caller;
		populate();
	}

	public void addOverlay(OverlayItem overlay){
		myOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return myOverlays.get(i);
	}

	// Removes overlay item i
	public void removeItem(int i){
		myOverlays.remove(i);
		populate();
	}

	public GeoPoint addTrain(TrainInfo _train)
	{
		// 
		// We need two overlays here, one for the train and one for the 
		// arrow
		//
		OverlayItem blah;
		GeoPoint where = new GeoPoint(
				(int) (_train.latitude * 1E6),
				(int) (_train.longititude * 1E6));


		int dirId = 0;
		/*
		 * Begin by determing which arrow we should use.. There are 8 of them (45 degrees each)
		 */

		int tick = ((int) Math.floor(_train.direction) + 44) / 45;
		if (tick == 8) tick = 0;
		switch (tick) {
		case 0: dirId = R.drawable.arrow_north; break;
		case 1: dirId = R.drawable.arrow_northeast; break;
		case 2: dirId = R.drawable.arrow_east; break;
		case 3: dirId = R.drawable.arrow_southeast; break;
		case 4: dirId = R.drawable.arrow_south; break;
		case 5: dirId = R.drawable.arrow_southwest; break;
		case 6: dirId = R.drawable.arrow_west; break;
		case 7: dirId = R.drawable.arrow_northwest; break;
		}		
		if (_train.speed > 10.0) {
			BitmapDrawable rawArrow = (BitmapDrawable) res.getDrawable(dirId);
			Bitmap thisOne = rawArrow.getBitmap();
			double factor = 0;
			if (_train.speed > 10.0) factor = 1.0;
			if (_train.speed > 19.0) factor = 1.25;
			if (_train.speed > 29.0) factor = 1.5;
			if (_train.speed > 39.0) factor = 1.75;
			if (_train.speed > 50.0) factor = 2.0;
			double newHeight = (double) thisOne.getHeight();
			newHeight *= factor;
			double newWidth = (double) thisOne.getWidth();
			newWidth *= factor;
			thisOne = Bitmap.createScaledBitmap(thisOne, (int) newWidth, (int) newHeight, true);

			BitmapDrawable thisArrow = new BitmapDrawable(thisOne);
			thisArrow.setBounds(0, 0, thisArrow.getIntrinsicWidth(), thisArrow.getIntrinsicHeight());
			boundCenter(thisArrow);
			blah = new OverlayItem(where, _train.getDescription(), _train.getDescription());
			blah.setMarker(thisArrow);
			addOverlay(blah);
		}

		BitmapDrawable thisTrain = (BitmapDrawable) res.getDrawable(R.drawable.train_image);
		thisTrain.setBounds(0, 0, thisTrain.getIntrinsicWidth(), thisTrain.getIntrinsicHeight());
		boundCenter(thisTrain);

		blah = new OverlayItem(where, _train.getDescription(), _train.getDescription());
		blah.setMarker(thisTrain);
		addOverlay(blah);
		return where;
	}

	@Override
	public void draw(android.graphics.Canvas canvas,
			MapView mapView,
			boolean shadow) {

		super.draw(canvas, mapView, false);

	}
	// Handle tap events on overlay icons
	@Override
	protected boolean onTap(int i){

		/*	In this case we will just put a transient Toast message on the screen indicating that we have
        captured the relevant information about the overlay item.  In a more serious application one
        could replace the Toast with display of a customized view with the title, snippet text, and additional
        features like an embedded image, video, or sound, or links to additional information. (The lat and
        lon variables return the coordinates of the icon that was clicked, which could be used for custom
        positioning of a display view.)*/

		OverlayItem xxx = myOverlays.get(i);
		Toast.makeText(caller.myContext, xxx.getTitle(), Toast.LENGTH_LONG).show();
		return(true);
	}

	// Returns present number of items in list
	@Override
	public int size() {
		return myOverlays.size();
	}

}
